package com.example.selfservicestation.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.selfservicestation.R
import com.example.selfservicestation.bean.LockStateBean
import com.example.selfservicestation.databinding.ItemLockStateBinding
import com.example.selfservicestation.enums.LockStateEnum

class LockStateAdapter(private val dataList: List<LockStateBean>,private val itemClickListener: (LockStateBean) -> Unit): RecyclerView.Adapter<LockStateAdapter.LockStateViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LockStateViewHolder {
        val binding = ItemLockStateBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return LockStateViewHolder(binding,itemClickListener)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: LockStateViewHolder, position: Int) {
        holder.bindData(dataList[position])
    }

    inner class LockStateViewHolder(private val binding: ItemLockStateBinding ,private val itemClickListener: (LockStateBean) -> Unit): RecyclerView.ViewHolder(binding.root){
        @SuppressLint("SetTextI18n")
        fun bindData(lockStateBean: LockStateBean){
            binding.tvLock.text = "#${lockStateBean.index} ${if (lockStateBean.state == LockStateEnum.LOCK) "关闭" else "打开"}"
            val bgResId = if (lockStateBean.state == LockStateEnum.OPEN) R.drawable.ripple_co_10_fafad2 else R.drawable.ripple_co_10_ee7942
            binding.tvLock.setBackgroundResource(bgResId)
            binding.tvLock.setOnClickListener { itemClickListener(lockStateBean) }
        }
    }
}